package com.atguigu.eduservice.listener;

import com.aiguigu.commonutils.ResultCode;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.dto.subject.SubjectData;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.servicebase.exceptionhandler.GuLiException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @title: SubjectListener
 * @Author danghailiang
 * @Date: 2022/9/23 21:37
 * @Version 1.0
 */
public class SubjectListener extends AnalysisEventListener<SubjectData> {
    /**
     * SubjectListener不能交给spring管理
     * subjectService服务
     */
    public EduSubjectService subjectService;

    public SubjectListener() {
    }

    public SubjectListener(EduSubjectService subjectService) {
        this.subjectService = subjectService;
    }

    /**
     * 数据
     */
    public static List<EduSubject> list = new ArrayList<>();

    @Override
    public void invoke(SubjectData data, AnalysisContext context) {
        if(Objects.isNull(data)){
            throw  new GuLiException(ResultCode.ERROR, "文件为空");
        }
        EduSubject oneSubjectName = this.existOneSubjectName(subjectService, data.getOneSubjectName());
        // 一行一行读取，每次读取有两个值，第一个值：一级分类， 第二个值：二级分类
        if(Objects.isNull(oneSubjectName)){
            oneSubjectName = new EduSubject();
            oneSubjectName.setParentId("0");
            oneSubjectName.setTitle(data.getOneSubjectName());
            // 进行添加
            subjectService.save(oneSubjectName);
        }
        String parentId = oneSubjectName.getId();
        EduSubject twoSubjectName = this.existTwoSubjectName(subjectService, data.getTwoSubjectName(), parentId);
        if(Objects.isNull(twoSubjectName)){
            twoSubjectName = new EduSubject();
            twoSubjectName.setParentId(parentId);
            twoSubjectName.setTitle(data.getTwoSubjectName());
            // 进行添加
            subjectService.save(twoSubjectName);
        }

    }

    /**
     * 去重：判断一级分类不能重复添加
     * @param subjectService 服务
     * @param subjectName   姓名
     * @return
     */
    private EduSubject existOneSubjectName(EduSubjectService subjectService, String subjectName){
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", "0")
                .eq("title", subjectName);
        return subjectService.getOne(wrapper);
    }

    /**
     * 去重：判断二级分类不能重复添加
     * @param subjectService 服务
     * @param subjectName   姓名
     * @return
     */
    private EduSubject existTwoSubjectName(EduSubjectService subjectService, String subjectName, String pid){
        LambdaQueryWrapper<EduSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduSubject::getParentId, pid)
                .eq(EduSubject::getTitle, subjectName);
        return subjectService.getOne(wrapper);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }
}
